<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="98px"
      >
        <el-col :span="12">
          <el-form-item label="出货单号" prop="ctmDjbh">
            <el-input
              v-model="queryParams.ctmDjbh"
              placeholder="请输入出货单号"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="ctmWldm">
            <el-input
              v-model="queryParams.ctmWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="出库仓库" prop="ckdm">
            <el-select
              v-model="queryParams.ckdm"
              multiple
              placeholder="请选择出库仓库"
            >
              <el-option
                v-for="item in allStorageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="退货厂商" prop="ctmCsdm">
            <el-select
              v-model="queryParams.ctmCsdm"
              placeholder="请选择退货厂商"
              filterable
            >
              <el-option
                v-for="item in allCsOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="出库日期">
            <el-date-picker
              v-model="daterangeCtmCkrq"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          @click="handleSearch"
          size="small"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExcel(1)"
          v-hasPermi="['output:ctmReport:export']"
          size="small"
        >
          明细数据
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExcel(2)"
          v-hasPermi="['output:ctmReport:export']"
          size="small"
        >
          物料统计
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExcel(3)"
          v-hasPermi="['output:ctmReport:export']"
          size="small"
        >
          日期统计
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExcel(4)"
          v-hasPermi="['output:ctmReport:export']"
          size="small"
        >
          厂商统计
        </el-button>
      </el-col>
    </el-row>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细数据" name="first">
        <el-table
          border
          stripe
          :height="tableH"
          @scrollBottom="load1"
          :data="ctmMstrList"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column label="#" width="40" type="index" align="center" />
          <el-table-column
            label="退货单号"
            :show-overflow-tooltip="true"
            width="135"
            align="center"
            prop="ctmDjbh"
          />
          <el-table-column
            label="退货日期"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="ctmCtrq"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.ctmCtrq, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="客户代码"
            :show-overflow-tooltip="true"
            width="120"
            align="center"
            prop="itmCustwldm"
          ></el-table-column>
          <el-table-column
            label="退货厂商"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="ctmCsdm"
          >
            <template #default="scope">
              <dict-tag :options="allCsOptions" :value="scope.row.ctmCsdm" />
            </template>
          </el-table-column>
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            width="135"
            align="center"
            prop="ctmWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            width="400"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="存放位置"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="ctmCwdm"
          >
            <template #default="scope">
              <dict-tag :options="allCwOptions" :value="scope.row.ctmCwdm" />
            </template>
          </el-table-column>
          <el-table-column
            label="退货数量"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="ctmCksl"
          />
          <el-table-column
            label="退货原因"
            :show-overflow-tooltip="true"
            width="120"
            align="center"
            prop="ctmThyy"
          />
          <el-table-column
            label="撤回原因"
            :show-overflow-tooltip="true"
            width="120"
            align="center"
            prop="ctmNgdesc"
          />
          <el-table-column
            label="退货状态"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="ctmZtbz"
          >
            <template #default="scope">
              <dict-tag :options="ctmZtbzOptions" :value="scope.row.ctmZtbz" />
            </template>
          </el-table-column>
          <el-table-column
            label="建立人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="ctmJlry"
          />
          <el-table-column
            label="建立日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="ctmJlrq"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.ctmJlrq, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="审核人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="ctmShry"
          />
          <el-table-column
            label="审核日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="ctmShrq"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.ctmShrq, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="批准人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="ctmPzry"
          />
          <el-table-column
            label="批准日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="ctmPzrq"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.ctmPzrq, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="过账人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="ctmGzry"
          />
          <el-table-column
            label="过账日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="ctmGzrq"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.ctmGzrq, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按物料统计" name="second">
        <el-table border stripe :height="tableH">
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="物料代码"
            align="center"
            prop="ctmWldm"
            width="135"
          />
          <el-table-column
            label="品名规格"
            align="center"
            prop="ctmPmgg"
            width="280"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="60"
            align="center"
            prop="ctmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.ctmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="退货数量"
            align="center"
            prop="ctmSqsl"
            width="100"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按日期统计" name="third">
        <el-table border stripe :height="tableH">
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="过账日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="chmGzrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.chmGzrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="物料代码"
            align="center"
            prop="ctmWldm"
            width="135"
          />
          <el-table-column
            label="品名规格"
            align="center"
            prop="ctmPmgg"
            width="280"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="60"
            align="center"
            prop="ctmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.ctmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="退货数量"
            align="center"
            prop="ctmSqsl"
            width="100"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按厂商统计" name="foruth">
        <el-table border stripe :height="tableH">
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="厂商名称"
            :show-overflow-tooltip="true"
            width="140"
            align="center"
            prop="cjmCsdm"
          >
            <template #default="scope">
              <dict-tag :options="allCsOptions" :value="scope.row.cjmCsdm" />
            </template>
          </el-table-column>
          <el-table-column
            label="物料代码"
            align="center"
            prop="ctmWldm"
            width="135"
          />
          <el-table-column
            label="品名规格"
            align="center"
            prop="ctmPmgg"
            width="280"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="60"
            align="center"
            prop="ctmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.ctmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="退货数量"
            align="center"
            prop="ctmSqsl"
            width="100"
          />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { ctmReportList, ctmReportExcel } from "@/api/output/backOutStart";
import { getAllCs } from "@/api/putStorage/putStorageAdd";
import { getAll } from "@/api/repertory/Storage";

export default {
  name: "CtmReport",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表格高度
      tableH: document.body.clientHeight - 300,
      //多表格默认
      activeTab: "first",
      // 显示搜索条件
      openSearch: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 建立日期时间范围
      daterangeCtmCkrq: [
        this.$moment().startOf("month").toDate(),
        this.$moment().endOf("month").toDate(),
      ],
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        ctmDjbh: null,
        ctmWldm: null,
        ckdm: null,
        ctmCsdm: null,
        index: 0,
      },
      /** 表格数据 */
      // 明细数据条数
      total1: 0,
      //明细数据字典
      ctmMstrList: [],
      /** 字典 **/
      //所有厂商字典
      allCsOptions: [],
      //所有仓库名称字典
      allStorageOptions: [],
      //所有默认储位字典
      allCwOptions: [],
      //单位字典
      unitFlagOptions: [],
      // 退货状态字典
      ctmZtbzOptions: [
        { dictValue: "1", dictLabel: "新增", listClass: "default" },
        { dictValue: "2", dictLabel: "审核", listClass: "default" },
        { dictValue: "3", dictLabel: "批准", listClass: "default" },
        { dictValue: "6", dictLabel: "过账", listClass: "default" },
      ],
    };
  },
  created() {
    this.getList();
    //单位字典
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    //默认储位字典
    getAll().then((response) => {
      this.allCwOptions = response.data;

      for (let item2 of this.allCwOptions) {
        item2.dictLabel = item2.storageDefaultstorage;

        item2.listClass = "default";
        item2.dictValue = item2.storageId + "";
      }
    });
    //所有仓库字典
    getAll().then((response) => {
      this.allStorageOptions = response.data;

      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    //获取所有厂商字典
    getAllCs().then((response) => {
      this.allCsOptions = response.data;
      for (let item of this.allCsOptions) {
        item.dictLabel = item.csmCsmc;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });
  },
  methods: {
    /** 查询来料进货入库开单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCtmCkrq && "" != this.daterangeCtmCkrq) {
        this.queryParams.params["beginCtmJlrq"] = this.moment(
          this.daterangeCtmCkrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endCtmJlrq"] = this.moment(
          this.daterangeCtmCkrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      ctmReportList(this.queryParams).then((response) => {
        this.ctmMstrList = [];
        //明细数据
        this.ctmMstrList = this.ctmMstrList.concat(response.data.detail.rows);
        this.total1 = response.data.detail.total1;
      });
    },
    load1() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total1 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 25;
        this.queryParams.pageNum++;
        ctmReportList(this.queryParams).then((response) => {
          //明细数据
          this.ctmMstrList = this.ctmMstrList.concat(response.data.detail.rows);
          this.total1 = response.data.detail.total1;
        });
      }
      return 0;
    },
    handleSearch() {
      this.openSearch = true;
      this.title = "查询退货出库统计分析";
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 导出Excel */
    handleExcel(index) {
      this.queryParams.index = index;
      const queryParams = this.queryParams;
      ctmReportExcel(queryParams).then((response) => {
        this.download(response.msg);
        this.queryParams.index = 0;
      });
    },
  },
};
</script>
